#include "DirectionalLightEffect.h"

DirectionalLightEffect::DirectionalLightEffect(const LPDIRECT3DDEVICE9 device, EffectManager *effMgr, TextureManager *texMgr) :Effect(device, effMgr, texMgr)
{
}

DirectionalLightEffect::~DirectionalLightEffect(void)
{
}

void DirectionalLightEffect::renderDirectionalLight(Camera *camera, ModelMesh *quad, D3DXVECTOR4 direction, D3DXVECTOR4 color, LPDIRECT3DSURFACE9 prevSurf)
{
	device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	device->SetRenderTarget(0, *texMgr->getSurface(L"lightMap"));
	
	effMgr->setupEffect(DEF_DIRECTIONAL_LIGHT_FX);
	effMgr->getSetEffect()->SetTexture("colorMap" , texMgr->getTexture(L"colorMap"));
	effMgr->getSetEffect()->SetTexture("normalMap", texMgr->getTexture(L"normalMap"));
	effMgr->getSetEffect()->SetTexture("depthMap" , texMgr->getTexture(L"depthMap"));

	effMgr->getSetEffect()->SetVector("cameraPosition", &camera->getPosition4());
	effMgr->getSetEffect()->SetMatrix("InvertViewProjection", &camera->getInvViewProjMatrix());
	effMgr->getSetEffect()->SetVector("halfPixel", &commonValues::halfPixel);
	effMgr->getSetEffect()->SetVector("lightDirection", &direction);
	effMgr->getSetEffect()->SetVector("Color", &color);

	UINT numPasses;
	device->BeginScene();
	effMgr->getSetEffect()->Begin(&numPasses, 0);
	effMgr->getSetEffect()->BeginPass(0);
	quad->getMesh()->DrawSubset(0);
	effMgr->getSetEffect()->EndPass();
	effMgr->getSetEffect()->End();
	device->EndScene();

	device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
	device->SetRenderTarget(0, prevSurf);
}
